<?php

namespace App\Store\Models\Order;

use App\Common\Services\Common;
use App\Common\Services\Date\TimerRange;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Member\Member;
use App\Store\Models\Account\Account;

class OrderPointschangebalance extends Model
{
    protected $table = 'site_order_pointschangebalance';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];
    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['memberinfo','shoppingaccountinfo1','shoppingaccountinfo2','storeinfo'])->where(['cp_id' => $cp_id])->orderBy('create_at', 'desc');

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $ids=$request->ids;
        $ids=is_array($ids)?$ids:(is_array(json_decode($ids,true))?json_decode($ids,true):[]);
        if(count($ids)>0){
            $query->whereIn('id',$ids);
        }

        $real_name = $request->real_name;
        if ($real_name != '') {
            $ids=Member::where('real_name', 'like', '%' . $real_name . '%')->where('is_del',1)->pluck('id');
            $query->whereIn('mem_id',$ids);
        }
        $is_maker = intval($request->is_maker);//挂单
        if ($is_maker>0) {
            $query->where('is_maker',$is_maker);
        }

        $mobile = $request->mobile;
        if ($mobile != '') {
            $ids=Member::where('mobile', 'like', '%' . $mobile . '%')->where('is_del',1)->pluck('id');
            $query->whereIn('mem_id',$ids);
        }


        $mem_card_no = $request->mem_card_no;
        if ($mem_card_no != '') {
            $ids=Member::where('mem_card_no', 'like', '%' . $mem_card_no . '%')->where('is_del',1)->pluck('id');
            $query->whereIn('mem_id',$ids);
        }

        $store_id = intval($request->store_id);//门店
        if ($store_id>0) {
            $query->where('store_id',$store_id);
        }

        $mem_id = intval($request->mem_id);//获取指定用户的积分记录
        if ($mem_id>0) {
            $query->where('mem_id',$mem_id);
        }

        $audit_status = intval($request->audit_status);//获取审核状态
        if ($audit_status>0) {
            $query->where('audit_status',$audit_status);
        }
        $order_sn=$request->order_sn;
        if ($order_sn != '') {
            $query->where('order_sn', 'like', '%' . $order_sn . '%');
        }

        $shopping_account_id1 = intval($request->shopping_account_id1);
        if ($shopping_account_id1>0) {
            $query->where('shopping_account_id1',$shopping_account_id1);
        }

        $shopping_account_id2 = intval($request->shopping_account_id2);
        if ($shopping_account_id2>0) {
            $query->where('shopping_account_id2',$shopping_account_id2);
        }
        $timerangetype=intval($request->timerangetype);//今日、本周、本月
        if(in_array($timerangetype,[1,2,3])){
            if($timerangetype==1){
                $time_res=TimerRange::getToday();
                $query->whereBetween('create_at',[$time_res['start_time'],$time_res['end_time']]);
            }

            if($timerangetype==2){
                $time_res=TimerRange::getThisWeek();
                $query->whereBetween('create_at',[$time_res['start_time'],$time_res['end_time']]);
            }

            if($timerangetype==3){
                $time_res=TimerRange::getThisMonth();
                $query->whereBetween('create_at',[$time_res['start_time'],$time_res['end_time']]);
            }
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    public function memberinfo()
    {
        return $this->hasOne(Member::class, 'id', 'mem_id')->where('is_del',1);
    }

    public function shoppingaccountinfo1(){

        return $this->hasOne(Account::class, 'id', 'shopping_account_id1');
    }
    public function shoppingaccountinfo2(){

        return $this->hasOne(Account::class, 'id', 'shopping_account_id2');
    }

    public function storeinfo(){

        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }
}
